//
//  CNKIScrollview.m
//  CNKICommunity
//
//  Created by wei on 17/2/8.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "CNKIScrollview.h"

@interface CNKIScrollview()

@property(nonatomic,weak)UIButton *SelectedBtn;
@property(nonatomic,strong)UIView *VerticalView;
@end


@implementation CNKIScrollview

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
}
-(void)setButtonContent:(NSArray *)ButtonContent
{
    _ButtonContent = ButtonContent;
    
    UIView *VerView = [UIView new];
    
    CGFloat BtnW = self.width;
    CGFloat BtnH = 40;
    CGFloat HorizonWidth = 0;
    CGFloat FirstBtnW = 0;
    
    if (_mode == CNKIScrollviewVertical)
    {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }

    
    for (int i = 0; i < ButtonContent.count; i++)
    {
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (_mode == CNKIScrollviewHorizon)
        {
          
            if (_WidthMode == CNKIScrollviewEqualWidth)
            {
               clickBtn.frame = CGRectMake(i * (SCREENWIDTH / _ButtonDisplayNum), 0, SCREENWIDTH / _ButtonDisplayNum, BtnH);
            }
            else
            {
            CGSize maxSize = CGSizeMake(MAXFLOAT, BtnH);
            CGFloat SubBtnW = [ButtonContent[i] boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : _TitleFont} context:nil].size.width;
            
            clickBtn.frame = CGRectMake(HorizonWidth, 0, SubBtnW, BtnH);
            
            HorizonWidth += (SubBtnW + 5);
            
            if (i == 0)
            {
                //记录第一个按钮的宽度
                FirstBtnW = SubBtnW;
            }
          }
            
        }
        else
        {
            clickBtn.frame = CGRectMake(0, i * BtnH, BtnW, BtnH);
            clickBtn.titleLabel.numberOfLines = 0;
        }
        
        
        
        clickBtn.tag = i + 100;
        [clickBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [clickBtn setTitle:ButtonContent[i] forState:UIControlStateNormal];
        
        [clickBtn setFont:_TitleFont];
        
        [clickBtn setTitleColor:_DeSelectTitleColor forState:UIControlStateNormal];
        [clickBtn setTitleColor:_SelectTitleColor forState:UIControlStateSelected];
        [clickBtn setBackgroundColor:[UIColor grayColor]];
        
        [clickBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        
        clickBtn.titleLabel.textAlignment = 1;
       
        [self addSubview:clickBtn];
        if (i == 0){
            self.SelectedBtn = clickBtn;
            self.SelectedBtn.selected = YES;
            [self reponseDelegate:self.SelectedBtn];
        };
    }
    
    
    if (_mode == CNKIScrollviewHorizon)
    {
        if (_WidthMode == CNKIScrollviewEqualWidth)
        {
            FirstBtnW = SCREENWIDTH / _ButtonDisplayNum;
            HorizonWidth = ButtonContent.count *FirstBtnW;
            
        }
        self.contentSize = CGSizeMake(HorizonWidth, BtnH + _IndicateHeight);
        VerView = [[UIView alloc]initWithFrame:CGRectMake(0, BtnH, FirstBtnW, _IndicateHeight)];
    }
    else
    {
        self.contentSize = CGSizeMake(self.width, ButtonContent.count * BtnH);
        VerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _IndicateHeight, BtnH)];
    }

    VerView.backgroundColor = _IndicateBackgroudColor;
    [self addSubview:VerView];
    VerView.tag = 1000;
    self.VerticalView = VerView;

    
}
-(void)clickButton:(UIButton*)sender
{
    if (self.SelectedBtn != sender)
    {
        self.SelectedBtn.selected = !self.SelectedBtn.selected;
        self.SelectedBtn = sender;
        self.SelectedBtn.selected = YES;
        
        if (_mode == CNKIScrollviewHorizon)
        {
           [UIView animateWithDuration:0.35 animations:^{
               self.VerticalView.x = sender.x;
               self.VerticalView.width = sender.width;
               
               CGFloat MoveOffset = CGRectGetMidX(sender.frame) - SCREENWIDTH / 2;
               if (MoveOffset <= 0)
               {
                   MoveOffset = 0;
               }
               if (MoveOffset >= self.contentSize.width - SCREENWIDTH + 50)
               {
                   MoveOffset = self.contentSize.width - SCREENWIDTH + 50;
               }
               [self setContentOffset:CGPointMake(MoveOffset, 0)];

               
           } completion:^(BOOL finished) {
               
           }];
           
            
            
        }
        else
        {
          self.VerticalView.y = (sender.tag - 100) * 40;
        }
        [self reponseDelegate:sender];
        
        
        
    }
   
}
-(void)reponseDelegate:(UIButton*)sender
{
    if ([_CNKIScrollviewDelegate respondsToSelector:@selector(clickBtnId:BtnName:)] && _mode == 1)
    {
        [_CNKIScrollviewDelegate clickBtnId:sender.tag - 100 BtnName:self.ButtonContent[sender.tag - 100]];
    }
    
    if ([_CNKIScrollviewDelegate respondsToSelector:@selector(clickHorizonBtnId:BtnHorizonName:)] && _mode == 0)
    {
        [_CNKIScrollviewDelegate clickHorizonBtnId:sender.tag - 100 BtnHorizonName:self.ButtonContent[sender.tag - 100]];
    }
 
}
-(void)setTitleFont:(UIFont *)TitleFont
{
    _TitleFont = TitleFont;
}
-(void)setSelectTitleColor:(UIColor *)SelectTitleColor
{
    _SelectTitleColor = SelectTitleColor;
}

-(void)setDeSelectTitleColor:(UIColor *)DeSelectTitleColor
{
    _DeSelectTitleColor = DeSelectTitleColor;
}
-(void)setMode:(CNKIScrollviewMode)mode
{
    _mode = mode;
}
-(void)setWidthMode:(CNKIScrollviewWidthMode)WidthMode
{
    _WidthMode = WidthMode;
}
-(void)setIndicateHeight:(CGFloat)IndicateHeight
{
    _IndicateHeight = IndicateHeight;
}
-(void)setIndicateBackgroudColor:(UIColor *)IndicateBackgroudColor
{
    _IndicateBackgroudColor = IndicateBackgroudColor;
}
-(void)setButtonDisplayNum:(CGFloat)ButtonDisplayNum
{
    _ButtonDisplayNum = ButtonDisplayNum;
}







@end
